<?php

namespace App\Http\Controllers\System;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use DB;
use App\Http\ClassBox\globals;
class OperationController extends Controller
{
    

    public function index()
    {
    $CliqueId_query=DB::table('business')->select('id','Name','Type')->where('Type',2)->get();//获取所有的集团
    $CliqueId_list=DB::table('business')->select('id','Name','Type')->where('Type',2)->where('Del',1)->get();//集团
    if($CliqueId_list!=NULL){
       $ComId_list=DB::table('business')->select('id','Name','Type')->where('Type',4)->where('Del',1)->where('CliqueId',$CliqueId_list[0]->id)->get();//公司
    }else{
    $CliqueId_list=NULL;
    }
    if($ComId_list!=NULL){
    $DepId_list=DB::table('business')->select('id','Name','Type')->where('Type',6)->where('Del',1)->where('ComId',$ComId_list[0]->id)->get();//部门
    }else{
    $ComId_list=NULL;
    }
    $Group_list="";
    if($DepId_list!=NULL){
    $Group_list=DB::table('business')->select('id','Name','Type')->where('Type',8)->where('Del',1)->where('DepId',$DepId_list[0]->id)->get();//业务线
    }else{
     $DepId_list=Null;

    }
    $banner="";
    $query_show=$this->q_show(0,$banner);
        return view('system.operation',[
               'CliqueId_query'=>$CliqueId_query,
               'CliqueId_list'=>$CliqueId_list,
               'ComId_list'=>$ComId_list,
               'DepId_list'=>$DepId_list,
               'Group_list'=>$Group_list,
               'query_show'=>$query_show

            ]);
    }
    /*
    *  搜索 select
    *  deng
    */
    public function ajax_query(Request $request){
        switch($request->input('Type'))
        {
        case 2:
        $data['data']=DB::table('business')->select('id','Name','Type')->where('CliqueId',$request->input('pid'))->where('Type',4)->get();
        return json_encode($data);
        break;
        case 4:
        $data['data']=DB::table('business')->select('id','Name','Type')->where('ComId',$request->input('pid'))->where('Type',6)->get();
        return json_encode($data);
        break;
        case 6:
        $data['data']=DB::table('business')->select('id','Name','Type')->where('DepId',$request->input('pid'))->where('Type',8)->get();
        return json_encode($data);
        break;
        default:
        return 0;
        }

    }
    /*
    *  添加 select
    *
    *  deng
    */
    public function ajax_add(Request $request){
       switch($request->input('Type'))
        {
        case 2:
        $data['data']=DB::table('business')->select('id','Name','Type')->where('CliqueId',$request->input('pid'))->where('Type',4)->where('Del',1)->get();//返回公司
        $data['Dep']="";
        if($data['data']!=NULL){
         $data['Dep']=DB::table('business')->select('id','Name','Type')->where('ComId',$data['data'][0]->id)->where('Type',6)->where('Del',1)->get();   
        }
       $data['Group']="";
       if($data['Dep']!=NULL){
         $data['Group']=DB::table('business')->select('id','Name','Type')->where('DepId',$data['Dep'][0]->id)->where('Type',8)->where('Del',1)->get();
       
        }
        return json_encode($data);
        break;
        case 4:
        $data['data']=DB::table('business')->select('id','Name','Type')->where('ComId',$request->input('pid'))->where('Type',6)->where('Del',1)->get();
        $data['Group']="";
        if($data['data']!=NULL){
        $data['Group']=DB::table('business')->select('id','Name','Type')->where('DepId',$data['data'][0]->id)->where('Type',8)->where('Del',1)->get();
        }
        return json_encode($data);
        break;
        case 6:
        $data['data']=DB::table('business')->select('id','Name','Type')->where('DepId',$request->input('pid'))->where('Type',8)->where('Del',1)->get();
        return json_encode($data);
        break;
        default:
        return 0;
        }

    }
    
  

    /**
     * 添加|修改  运营管理
     *
     * deng
     */
    public function create(Requests\System\OperationRequest $request)
    {
    
      if(intval($request->input('id'))>0){
        DB::table('operate')->where('id','=',intval($request->input('id')))->update(
            array(
                'CliqueId'=>intval($request->input('CliqueId')),
                'ComId'=>intval($request->input('ComId')),
                'DepId'=>intval($request->input('DepId')),
                'GroupId'=>intval($request->input('GroupId')),
                'Leader'=>intval($request->input('Leader')),//是否为接口人
                'Name'=>trim($request->input('Name')),
                'WW'=>$request->input('WW'),
                'QQ'=>$request->input('QQ'),
                'call'=>$request->input('call'),//分机
                'Email'=>$request->input('Email'),
                'Mobile'=>$request->input('Mobile'),//手机
                'UseId'=>0,
                'Time'=>date('Y-m-d H:i:s',time()),
                'Del'=>1
            )
        );

          return redirect('system/operation/index')->with('successful','修改成功！');
      }else{

       $insert=array(
                'CliqueId'=>intval($request->input('CliqueId')),
                'ComId'=>intval($request->input('ComId')),
                'DepId'=>intval($request->input('DepId')),
                'GroupId'=>intval($request->input('GroupId')),
                'Leader'=>intval($request->input('Leader')),//是否为接口人
                'Name'=>trim($request->input('Name')),
                'WW'=>$request->input('WW'),
                'QQ'=>$request->input('QQ'),
                'call'=>$request->input('call'),//分机
                'Email'=>$request->input('Email'),
                'Mobile'=>$request->input('Mobile'),//手机
                'UseId'=>0,
                'Time'=>date('Y-m-d H:i:s',time()),
                'Del'=>1
          );
         DB::table('operate')->insert($insert);
         return redirect('system/operation/index')->with('successful','添加成功！');
    }
}
     /**
     * 恢复与停用
     * deng
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function restore_disable (Request $request)
    {
       $result=array();
       $Del=intval($request->input('Del'));
       $id=intval($request->input('id'));

       if(empty($Del) && empty($id)){

        $result['Success'] = 'false';
        return json_encode($result);
       }
       if($Del=='1' && $id!=NULL){
           DB::table('operate')->where('id',$id)->update(
             array(
               'Del' =>0
                )
               );

       }

       if($Del=='0' && $id!=NULL){
           DB::table('operate')->where('id',$id)->update(
             array(
               'Del' =>1
                )
               );

       }

        $result['Success'] = 'true';
        return json_encode($result);
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * 编辑按钮
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit(Request $request)
    {
        $result=array();
        $id=$request->input('id');
        if(empty($id)){
        $result['masege'] = 'false';
        return json_encode($result);
        }
        $result['data']=DB::table('operate')->where('id',$id)->get();//获取当前运营人员的信息
        $result['CliqueId']=DB::table('business')->select('id','Name','Type')->where('id',$result['data'][0]->CliqueId)->get();//集团
        $result['ComId']=DB::table('business')->select('id','Name','Type')->where('CliqueId',$result['data'][0]->CliqueId)->get();//公司
        $result['DepId']=DB::table('business')->select('id','Name','Type')->where('ComId',$result['data'][0]->ComId)->where('Type',6)->get();//部门
        $result['GroupId']=DB::table('business')->select('id','Name','Type')->where('DepId',$result['data'][0]->DepId)->where('Type',8)->get();//组 
       return json_encode($result);
    }



      /**
     * 分页---接口
     *  deng
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show_list(Request $request)
    {
        $result=array();
        $index= $request->input('index');

       //  搜索条件
       $banner=""; //拼接
       $CliqueId=$request->input('CliqueId');//集团
       if($CliqueId!=NULL){
       $banner .=" and operate.CliqueId=".$CliqueId;
       }
       $ComId=$request->input('ComId');//公司
       if($ComId!=NULL){
       $banner .=" and operate.ComId=".$ComId;
       }
       $DepId=$request->input('DepId');//部门
       if($DepId!=NULL){
       $banner .=" and operate.DepId=".$DepId;
       }
       $GroupId=$request->input('GroupId');//组
       if($GroupId!=NULL){
           $banner .=" and operate.GroupId=".$GroupId;
       }
       $Name=trim($request->input('Name'));//姓名
       if($Name!=NULL){
         $banner .=" and operate.Name regexp '".$Name."'";
       }
       if($Name!=NULL){
         $banner .=" or operate.WW regexp '".$Name."'";
       }
       $query_list=$this->q_show($index,$banner);
       $result["Is_null"] ="true";
       $result["data"] = $query_list;
       return json_encode($result);
      

    }

  /**
  * 运营管理页面
  *  deng
  *
  */
  public function q_show($index,$banner){
       $result=array();
        $number=10;// 显示20条信息

        $index=$index ? ($index-1)*$number+1 : 1;
        $index--;
        $query_cost=DB::select('select operate.*,business.Name as b_name 
            from operate LEFT JOIN 
            business ON if(operate.GroupId=0,operate.DepId=business.id,operate.GroupId=business.id) where 1=1'.$banner);

        $cost_list=array_reverse($query_cost);  //打乱原来数组的key  生成一个新的数组
        $result['total']=count($cost_list);
        $result['list']=array_slice($cost_list,$index,$number);
        return $result;


  }


     /*
     *  查看运营详情
     *  deng
     *
     */
     public function See_details(Request $request){

        $id=$request->input('id');
        $result=array();
        $result['data']=DB::table('operate')->where('id',$id)->get();
        $result['CliqueId']=DB::table('business')->select('id','Name','Type')->where('id',$result['data'][0]->CliqueId)->get();//集团
        $result['ComId']=DB::table('business')->select('id','Name','Type')->where('id',$result['data'][0]->ComId)->where('Type',4)->get();//公司
        $result['DepId']=DB::table('business')->select('id','Name','Type')->where('id',$result['data'][0]->DepId)->where('Type',6)->get();//部门
        $result['GroupId']=DB::table('business')->select('id','Name','Type')->where('id',$result['data'][0]->GroupId)->where('Type',8)->get();//组 
        return json_encode($result);

     }

}
